<template>
  <a-card :bordered="false" :title="title">
    <a-form :form="form">
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="上一级名称">
        <a-input disabled v-decorator="['parentName', {}]"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="名称">
        <a-input placeholder="请输入物资大类名称" v-decorator="['name',validatorRules.name]"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="编码">
        <a-input placeholder="请输入物资大类编码" v-decorator="['code', validatorRules.code]"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="别名">
        <a-input placeholder="请输入物资大类别名" v-decorator="['alias',{}]"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="排序">
        <a-input-number :max="999999999" :min = "0" placeholder="请输入排序序号" style="width:100%" v-decorator="[ 'sort', {}]"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="资质">
        <a-input placeholder="请输入物资大类别名" v-decorator="['qualification',{}]"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="描述">
        <a-input placeholder="请输入物资大类别名" v-decorator="['description',{}]"/>
      </a-form-item>
    </a-form>
    <div style="text-align: center">
      <a-button @click="handleCancel"  type="default" icon="sync" style="margin-right: 8px">重置</a-button>
      <a-button @click="handleOk" type="primary" icon="form">保存</a-button>
    </div>
  </a-card>
</template>

<script>
  import pick from 'lodash.pick'
  import { postAction } from '../../../../api/manage'
  export default {
    name: 'MdmMatCatModule',
    data(){
      return{
        form: this.$form.createForm(this),
        title:'',
        editSign:false,
        parentId:'',
        matCat:null,
        //表单验证规则
        validatorRules: {
          code: {
            rules: [{ required: true, message: '请输入物资大类编码!' }]
          },
          name: {
            rules: [{ required: true, message: '请输入物资大类名称!' }]
          }
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        url:{
          addOrUpdate:'/mdm/matCat/addOrUpdate',
        }
      }
    },
    methods:{
      /**
       * @author pqz
       * @description 初始化方法
       **/
      openAdd(parentId, title, parentName) {
        debugger
        this.title = title
        this.handleCancel()
        let params = {
          parentName: parentName
        }
        //设置父名称
        this.form.setFieldsValue(pick(params, 'parentName'))
        this.parentId = parentId
      },

      /**
       * @author pqz
       * @description 单击每行树数据事件
       **/
      setValue(row) {
        this.matCat = row
        this.form.setFieldsValue(pick(row,'id','sort', 'name','code','alias','qualification','description','parentName','parentId'))
      },

      /**
       * @author pqz
       * @description 重置
       */
      handleCancel(){
        this.matCat = null
        this.form.resetFields()
      },

      /**
       * @author pqz
       * @description 保存方法
       */
      handleOk(){
        this.form.validateFields((err,values)=>{
          if (!err) {
            //编辑情况
            if (this.matCat!=null && this.matCat.id != null ){
              values.id = this.matCat.id
            } else {
              //新增时使用
              values.parentId = this.parentId
            }
            postAction(this.url.addOrUpdate,values).then((res)=>{
              if (res.success){
                this.$message.success(res.result)
                this.handleCancel()
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>